package org.feiye.studyday28.case4;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketAddress;

public class LoginSever {

	public static void main(String[] args) {
		// 服务器端
		// 准备了一个空包
		byte[] bytes = new byte[1024];
		DatagramPacket dg = new DatagramPacket(bytes, bytes.length);
		// 快递点
		DatagramSocket socket = null;
		try {
			socket = new DatagramSocket(5000);
			// 在快递点取礼物
			socket.receive(dg);
			// 拆礼物
			String info = new String(dg.getData(), 0, dg.getData().length);
			System.out.println("客户端响应：" + info);

			// 给客户端一个响应
			String reply = "一双AJ";
			byte[] bytes2 = reply.getBytes();
			// 获得客户端地址
			SocketAddress address = dg.getSocketAddress();
			// 封包
			DatagramPacket dg1 = new DatagramPacket(bytes2, 0, bytes2.length, address);
			// 发送
			socket.send(dg1);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (socket != null)
				socket.close();
		}
	}

}
